package com.it2297.view;

import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JButton;

import com.it2297.controller.ProductController;
import com.it2297.model.Order;
import com.it2297.model.Payment;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.JComboBox;

public class PaymentView extends JPanel {
	private final ProductController pc = new ProductController(this);
	private final static JTextField nricTxtField = new JTextField();
	private final JLabel lblEnterNric = new JLabel("Enter Order ID");
	private static JScrollPane scrollPane = new JScrollPane();
	private final JButton btnSearch = new JButton("Search");
	private static String nric;
	private final JButton btn_All = new JButton("Get All");
	private final JButton btn_sale = new JButton("Sale Chart");
	private final String [] Month = {"January", "February", "March", "April", "May", "June", "July", "August", "September" ,"October","November", "December"};
	private final JComboBox comboBox = new JComboBox(Month);
	/**
	 * Create the panel.
	 */
	public PaymentView() {
		this.setBounds(100, 100, 750, 570);
		this.add(getScrollpane());
		nricTxtField.setBounds(157, 11, 120, 25);
		nricTxtField.setColumns(10);
		initGUI();
	}
	private void initGUI() {
		setLayout(null);
		setLayout(null);
		
		add(nricTxtField);
		lblEnterNric.setBounds(10, 14, 137, 19);
		
		add(lblEnterNric);
		
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pc.clear();
				scrollPane.getViewport().removeAll();
				if(nricTxtField.getText().isEmpty()){
					JOptionPane.showMessageDialog(null, "Please type in Order ID!");
				}else{
						pc.clear();
				boolean valid = pc.setOrderlistItem(getID());
				if(valid){
				pc.addOrders();
				}else{
					JOptionPane.showMessageDialog(null, "No Order Found!");
				}
				}
			}
		});
		btnSearch.setBounds(287, 12, 89, 23);
		
		add(btnSearch);
		scrollPane.setBounds(10, 56, 700, 400);
		
		add(scrollPane);
		btn_All.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				pc.clear();
				scrollPane.getViewport().removeAll();
				boolean valid = pc.setUnpaid();
				if(valid){
				pc.addOrders();
				}else{
					JOptionPane.showMessageDialog(null, "No Order avaliable");
				}
			}
		});
		btn_All.setBounds(386, 12, 89, 23);
		
		add(btn_All);
		
		btn_sale.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String Month = comboBox.getSelectedItem().toString();
				int selectedMonth;
				switch(Month){
				case "January" :
					selectedMonth =1;
					break;
				case "February":
					selectedMonth =2;
					break;
				case "March":
					selectedMonth =3;
					break;
				case "April":
					selectedMonth =4;
					break;
				case "May":
					selectedMonth =5;
					break;
				case "June":
					selectedMonth =6;
					break;
				case "July":
					selectedMonth =7;
					break;
				case "August":
					selectedMonth = 8;
					break;
					
				case "September":
					selectedMonth =9;
					break;
				case "October":
					selectedMonth = 10;
					break;
				case "November":
					selectedMonth =11;
					break;
				case  "December":
					selectedMonth =12;;
					break;
				default :
					selectedMonth = 0;
					break;
				}
				pc.showPieChart(selectedMonth);
			}
		});
		
		btn_sale.setBounds(584, 12, 126, 23);
		
		add(btn_sale);
		comboBox.setBounds(485, 13, 89, 20);
		
		add(comboBox);
	}
	
	public void setNric(String id){
		this.nric = id;
	}
	public static int getID(){
		return Integer.parseInt(nricTxtField.getText());
	}
	public static void setPanel(JPanel panel) {
		getScrollpane().setViewportView(panel);
	}

	public static JScrollPane getScrollpane() {
		return scrollPane;
	}
}
